package com.syh.jn.scm.domain.upstream.entity.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.UpstreamNewGoodsRequest;
import com.syh.jn.scm.dto.upstream.base.UpBusinessEntityGoodsOfferMto;
import com.syh.jn.scm.dto.upstream.base.UpBusinessEntityGoodsOfferSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * 商品报价
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class UpBusinessEntityGoodsOffer extends AbstractSimpleAggregate<Long> {

  public UpBusinessEntityGoodsOffer(UpstreamNewGoodsRequest upstreamNewGoodsRequest) {
    super.initDefault();
    this.goodsId = upstreamNewGoodsRequest.getAssociatedGoodsId();
    this.businessEntityId = upstreamNewGoodsRequest.getBusinessEntityId();
    this.quantity = new BigDecimal("1.00");
    this.goodsPrice = new BigDecimal("0.00");
    this.maxSupply = new BigDecimal("9999.00");
    this.disabled();
  }

  /**
   * 新建商品报价
   *
   * @param sto
   */
  public UpBusinessEntityGoodsOffer(UpBusinessEntityGoodsOfferSto sto) {
    ExtBeanUtils.copyProperties(sto, this);
    //初始化数据
    super.initDefault();
    this.quantity = new BigDecimal("1.00");
    this.goodsPrice = new BigDecimal("0.00");
    this.maxSupply = new BigDecimal("9999.00");
    this.disabled();
    this.businessEntityId = sto.getBusinessEntityId();
  }

  /**
   * 修改商品报价
   *
   * @param mto
   */
  public void modify(UpBusinessEntityGoodsOfferMto mto) {
    ExtBeanUtils.copyProperties(mto, this, false);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 删除商品报价
   */
  public void destroy() {
    if (this.status.equals(Short.valueOf("1"))) {
      logger.error("modify:该报价处于启用状态,删除失败!");
      throw new BusinessException("该报价处于启用状态,删除失败!");
    }
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = 1;
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = -1;
  }

  /**
   * 商品id
   */
  private Long goodsId;
  /**
   * 起批量
   */
  private BigDecimal quantity;
  /**
   * 商品价格
   */
  private BigDecimal goodsPrice;
  /**
   * 最大供应量
   */
  private BigDecimal maxSupply;
  /**
   * 状态:-1：停用 1：启用
   */
  private Short status;
  /**
   * 归属id
   */
  private Long businessEntityId;
}
